package crivia.db.a;

public class Specially_Oracle extends Specially_Abstract {
	
	@Override
	public String toInt(String tSQL) {
		return "to_number(" + tSQL + ")";
	}

	/**> <b>to_char(</b>?<b>)</b>*/
	@Override
	public String toChar(String tSQL) {
		return "to_char(" + tSQL + ")";
	}

	/**> <b>to_date(</b>?<b>, 'yyyy-MM-dd')</b>*/
	@Override
	public String toDate(String tSQL) {
		return "to_date(" + tSQL + ", 'yyyy-MM-dd')";
	}

	/**> <b>to_date(</b>?<b>, 'yyyy-MM-dd hh24:mi:ss')</b>*/
	@Override
	public String toDateTime(String tSQL) {
		return "to_date(" + tSQL + ", 'yyyy-MM-dd hh24:mi:ss')";
	}

	/**> <b>sysdate</b>*/
	@Override
	public String today() {
		return "sysdate";
	}

	/**> <b>to_timestamp(</b>?<b>, 'yyyy-mm-dd hh24:mi:ss.ff</b>?<b>')</b>*/
	@Override
	public String toTimestamp(String tSQL, int n) {
		return "to_timestamp(" + tSQL + ", 'yyyy-mm-dd hh24:mi:ss.ff" + n + "')";
	}

	/**> <b>nvl(</b>?<b>, </b>?<b>)</b>*/
	@Override
	public String isNull(String tSQL, String tSQL2) {
		return "nvl(" + tSQL + ", " + tSQL2 + ")";
	}

	/**> <b>(nvl(</b>?<b>, 'FUCK ORACLE!') = 'FUCK ORACLE!')</b>*/
	@Override
	public String isNull(String tSQL) {
		return "(nvl(" + tSQL + ",'FUCK ORACLE!') = 'FUCK ORACLE!')";
	}

	/**> <b>(nvl(</b>?<b>, 'FUCK ORACLE!') != 'FUCK ORACLE!')</b>*/
	@Override
	public String isNotNull(String tSQL) {
		return "(nvl(" + tSQL + ",'FUCK ORACLE!') != 'FUCK ORACLE!')";
	}

	/**> <b>"</b>?<b>"</b>*/
	@Override
	public String keyWordString(String tSQL) {
		return "\"" + tSQL + "\"";
	}

	@Override
	public String hasTable(String tableName) {
		return "select 1 from user_tables where table_name = '"+tableName.toUpperCase()+"' ";
	}

	/**> <b>concat(</b>?<b>, </b>?<b>)</b>*/
	@Override
	public String concatString(String... strings) {
		if (null == strings || 0 == strings.length){
			return null;
		}
		if (1 == strings.length){
			return strings[0];
		}
		StringBuilder s = new StringBuilder();
		for (int k = 1; k < strings.length; k++){
			s.append("concat(");
		}
		s.append(strings[0]);
		for (int k = 1; k < strings.length; k++){
			s.append(",").append(strings[k]).append(")");
		}
		return s.toString();
	}

	@Override
	public String splitPage(String fields,String namedFields,String pk
			, String table, String cd
			, String groupBy, String orderBy
			, int pi, int ps) {
		boolean hasOrderBy = true
				&& null != orderBy 
				&& ! "".equals(orderBy);
		String[] orderBys = hasOrderBy ? orderBy.split(",") : new String[]{};
		StringBuilder sortedOrderBy = new StringBuilder();
		for (String o : orderBys){
			if ("".equals(o.trim())){
				continue;
			}
			String[] kws = o.trim().split(" ");
			sortedOrderBy.append(o);
			if (kws.length == 1){
				sortedOrderBy.append(" asc ");
			}
			sortedOrderBy.append(" , ");
		}
		orderBy = sortedOrderBy.toString();
		StringBuilder s = new StringBuilder("select ")
			.append(namedFields).append(" from ( select ")
			.append(namedFields).append(" , rownum rn from ( select ")
			.append(fields).append(" from ").append(table)
			.append(" where 1=1 and (").append(cd).append(") ")
			.append(" ").append(groupBy)
			.append(" order by ").append(orderBy).append(pk)
			.append(" asc ) spt1 where rownum <= ").append(pi * ps)
			.append(" ) spt2 where spt2.rn > ").append((pi - 1) * ps)
			.append(" order by ").append(orderBy).append(pk).append(" asc");
		return s.toString();
	}

	@Override
	public String splitPage(String fields,String pk
			, String table, String cd
			, String orderBy
			, int pi, int ps) {
		return splitPage(fields, fields, pk, table, cd, "", orderBy, pi, ps);
	}

	@Override
	public String splitPage(String fields , String pk
			, String table , String cd
			, int pi , int ps){
		return splitPage(fields, fields, pk, table, cd, "", "", pi, ps);
	}

	@Override
	public String splitPage(String fields , String pk
			, String table
			, int pi, int ps) {
		return splitPage(fields, fields, pk, table, "", "", "", pi, ps);
	}

	@Override
	public String indexOf(String sqlSource, String sqlTarget) {
		return "instr(" + sqlSource + " , " + sqlTarget + ")";
	}

	@Override
	public String subString(String tSQL, String begin, String end) {
		return "substr(" + tSQL + " , " + begin + " , " + end + ")";
	}
	
	@Override
	public String stringLength(String tSQL) {
		return "length(" + tSQL + ")";
	}

	@Override
	public String moneyFormatFloat2(String tSQL) {
		return toChar(tSQL+" , 'FM999,999,999,999,999.00'");
	}

}
